﻿using Common;
using IBLL;
using Microsoft.AspNetCore.Mvc;
using Models;
using Models.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Unmanned_MSUI.Areas.APP.Controllers
{
    [Area("APP")]
    public class ProductRecordController : Controller
    {
        #region 构造函数

        private IProductRecordBLL _productRecordBLL;
        private ICommodityInfoBLL _commodityInfoBLL;
        public ProductRecordController(
            IProductRecordBLL productRecordBLL
            , ICommodityInfoBLL commodityInfoBLL
            )
        {
            _productRecordBLL = productRecordBLL;
            _commodityInfoBLL = commodityInfoBLL;
        }
        #endregion

        public IActionResult Index()
        {
            return View();
        }

        #region 查询商品记录列表的接口--分页（GetProductRecord）
        /// <summary>
        /// 查询商品记录列表的接口--分页
        /// </summary>
        /// <param name="page">第几页</param>
        /// <param name="limit">每页几条数据</param>
        /// <param name="consumableId">商品记录Id</param>
        /// <param name="consumableName">商品名</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStaffInfo(int page, int limit, string consumableId, string consumableName)
        {
            //总条数
            int count;
            //调用BLL的获取商品记录的方法
            List<GetProductRecordDTO> list = _productRecordBLL.GetProductRecord(page, limit, consumableId, consumableName, out count);

            //返回结果
            ReturnResult result = new ReturnResult()
            {
                Code = 0,//layui要求状态码为0
                Msg = "获取成功！",
                Data = list,
                IsSuccess = true,
                Count = count
            };
            //返回结果
            return Json(result);
        }
        #endregion

        #region 添加商品记录的接口（CreateProductRecord）
        [HttpPost]
        public IActionResult CreateProductRecord([FromForm] ProductRecord product)
        {
            string msg;
            //调用添加方法
            bool isSuccess = _productRecordBLL.CreateProductRecord(product, out msg);
            //返回结果封装
            ReturnResult result = new ReturnResult();
            result.Msg = msg;
            result.IsSuccess = isSuccess;

            //判断是否成功
            if (isSuccess)
            {
                result.Code = 200;
            }
            return Json(result);
        }

        #endregion

        #region 更新商品记录信息的接口（UpdateProductRecord）

        [HttpPost]
        public IActionResult UpdateProductRecord([FromForm] ProductRecord productRecord)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //调用方法
            string msg;
            bool isOk = _productRecordBLL.UpdateProductRecord(productRecord, out msg);
            result.Msg = msg;//把返回的描述
            //判断是否成功
            if (isOk)
            {
                result.Code = 200;
                result.IsSuccess = isOk;
            }
            return Json(result);//返回结果
        }

        #endregion

        #region 商品记录软删除的方法（DeleteProductRecord）
        /// <summary>
        /// 商品记录软删除的接口
        /// </summary>
        /// <param name="id">前端传进来的Id</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteProductRecord(string id)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //非空判断
            if (string.IsNullOrWhiteSpace(id))
            {
                result.Msg = "Id不能为空！";
                return Json(result);
            }
            //调用删除方法
            bool isOk = _productRecordBLL.DeleteProductRecord(id);
            //判断删除情况
            if (isOk)
            {
                result.Msg = "删除成功！";
                result.Code = 200;
            }
            //返回结果
            return Json(result);
        }

        #endregion

        #region 商品记录批量软删除的接口（DeleteProductRecords）
        /// <summary>
        /// 批量员工软删除的接口
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteProductRecords(List<string> ids)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //非空判断
            if (ids == null || ids.Count == 0)
            {
                result.Msg = "你还没有选择要删除的商品记录！";
                return Json(result);
            }
            //批量删除的方法
            bool isOk = _productRecordBLL.DeleteProductRecords(ids);
            //判断是否成功
            if (isOk)
            {
                result.Msg = "删除成功！";
                result.Code = 200;
            }
            else
            {
                result.Msg = "删除失败！";
            }
            //返回结果
            return Json(result);
        }

        #endregion
    }
}
